package com.frogs.ucenter.controller;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.UserIntegralMoneyRelation;
import com.frogs.ucenter.service.UserIntegralMoneyRelationService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/admin/userIntegralMoneyRelation")
public class UserIntegralMoneyRelationController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/userIntegralMoneyRelation";

    private static final String HOME_VIEW = "admin/userIntegralMoneyRelation/home";

    private static final String EDIT_VIEW = "admin/userIntegralMoneyRelation/edit";
    
    private static final String QUERY = "/query";

    @Autowired
    private UserIntegralMoneyRelationService userIntegralMoneyRelationService;

    @RequestMapping()
    public String listUserIntegralMoneyRelation(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createUserIntegralMoneyRelation(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "model",userIntegralMoneyRelationService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveUserIntegralMoneyRelation(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserIntegralMoneyRelation userIntegralMoneyRelation) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        boolean flag = true;
        try {
            userIntegralMoneyRelationService.insert( userIntegralMoneyRelation ); 
            builder = DWZResponse.getSucessBuilder("新增成功");
        } catch (Exception e) {
        	flag = false;
            builder = DWZResponse.getFailBuilder("新增失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        request.setAttribute("flag", flag);
        return null;
    }

    @RequestMapping(SHOW)
    public String showUserIntegralMoneyRelation(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( userIntegralMoneyRelationService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateUserIntegralMoneyRelation(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserIntegralMoneyRelation userIntegralMoneyRelationNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        boolean flag = true;
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                UserIntegralMoneyRelation userIntegralMoneyRelation = userIntegralMoneyRelationService.getById( id ); 
                PropertyUtils.copyProperties(userIntegralMoneyRelation, userIntegralMoneyRelationNew);
                userIntegralMoneyRelationService.update( userIntegralMoneyRelation ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("更新成功");
            } catch (Exception e) {
            	flag = false;
                builder = DWZResponse.getFailBuilder("更新失败" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        request.setAttribute("flag", flag);
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteUserIntegralMoneyRelation(HttpServletRequest request, HttpServletResponse response, ModelMap model, @RequestParam(required=true, value="ids")String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        boolean flag = true;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            userIntegralMoneyRelationService.delete( model ); 
            builder = DWZResponse.getSucessBuilder("删除成功");
        } catch (Exception e) {
        	flag = false;
            builder = DWZResponse.getFailBuilder("删除失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        request.setAttribute("flag", flag);
        return null;
    }
    
    @RequestMapping(QUERY)
    public String queryUserIntegralMoneyRelation(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
    	HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer levelId = reqInfo.getIntParameter( "levelId", -1 );
        int isExit = 0;
        DWZResponse.Builder builder;
        try {
        	if(userIntegralMoneyRelationService.isExistsUserIntegralMoneyRelation(levelId)) {
        		isExit = 1;
        	}
        	builder = DWZResponse.getSucessBuilder("操作成功");
        } catch (Exception e) {
        	builder = DWZResponse.getFailBuilder("操作异常" + e.getMessage());
        }
        builder.addAttribute("isExit", isExit);
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
    }
    
    private PageList<UserIntegralMoneyRelation> getPageList(ModelMap model) {
        PageList<UserIntegralMoneyRelation> userIntegralMoneyRelationList = userIntegralMoneyRelationService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("userIntegralMoneyRelationList",userIntegralMoneyRelationList);
        model.put("pageTurn",userIntegralMoneyRelationList.getPageTurn());
        return userIntegralMoneyRelationList;
    }
}